import Taro from '@tarojs/taro';

import config from "../../config";
import bmap from '../../libs/bmap-wx';


export function requestLocation() {

    return new Promise((resolve, reject) => {
        if (Taro.getEnv() === Taro.ENV_TYPE.WEAPP) {
            let BMap = new bmap.BMapWX({
                ak: config.bdMapAk
            });
            let fail = (error) => {
                console.log(error)
                reject(error);
            }
            let success = (result) => {
                console.log(result)
                let addressComponent = result.originalData.result.addressComponent
                let city = addressComponent.city;
                let latitude = result.originalData.result.location.lat;
                let longitude = result.originalData.result.location.lng;
                resolve({ city, latitude, longitude })
            }
            BMap.regeocoding({ fail, success, iconPath: "", iconTapPath: "" })
        } else {

            let BMap = window.BMap;
            var geolocation = new BMap.Geolocation();

            geolocation.getCurrentPosition(function(r) {
                if (this.getStatus() == window.BMAP_STATUS_SUCCESS) {
                    var geoc = new BMap.Geocoder();
                    geoc.getLocation(r.point, function(rs) {
                        var addComp = rs.addressComponents;
                        let city = addComp.city;
                        let latitude = r.point.lat;
                        let longitude = r.point.lng;
                        resolve({ city, latitude, longitude })

                    });
                } else {

                }
            }, { enableHighAccuracy: true })
        }
    });

}




export default {
    requestLocation
}